package ihm;

import java.awt.EventQueue;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;

import metier.Collaborateur;
import metier.Contrat;

import java.awt.Dialog.ModalExclusionType;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

@SuppressWarnings("serial")
public class FenetreModifierTache extends JDialog {
	private JPanel contentPane;
	private JTextField txtChargeTache;
	private JTextField txtLibelleTache;
	private ArrayList<Collaborateur> lesCollaborateurs;
	private ArrayList<Contrat> lesContrats;
	private JComboBox<Contrat> cboContrat;
	private JComboBox<Collaborateur> cboCollaborateur;
	private JLabel lblValeurNumTache;

	/**
	 * Create the frame.
	 */
	public FenetreModifierTache() {
		setModal(true);
		setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE);
		setTitle("Modifier une tâche");
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 357, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JLabel lblNumTache = new JLabel("Numéro de tâche");
		
		JLabel lblLibellé = new JLabel("Libellé");
		
		JLabel lblChargePrevi = new JLabel("Charge prévisionnelle");
		
		JLabel lblCollaborateur = new JLabel("Collaborateur");
		
		JLabel lblContrat = new JLabel("Contrat");
		
		JButton btnValider = new JButton("Valider");
		
		JButton btnAnnuler = new JButton("Annuler");
		btnAnnuler.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		
		cboCollaborateur = new JComboBox<Collaborateur>();
		
		cboContrat = new JComboBox<Contrat>();
		
		txtChargeTache = new JTextField();
		txtChargeTache.setColumns(10);
		
		txtLibelleTache = new JTextField();
		txtLibelleTache.setColumns(10);
		
		lblValeurNumTache = new JLabel("New label");
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(21)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(lblLibellé)
						.addComponent(lblNumTache)
						.addComponent(lblChargePrevi)
						.addComponent(lblCollaborateur)
						.addComponent(lblContrat))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addComponent(txtChargeTache, GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
								.addComponent(txtLibelleTache, GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
								.addComponent(cboCollaborateur, 0, 180, Short.MAX_VALUE)
								.addComponent(cboContrat, 0, 180, Short.MAX_VALUE))
							.addGap(21))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(lblValeurNumTache)
							.addContainerGap())))
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(92)
					.addComponent(btnValider, GroupLayout.DEFAULT_SIZE, 65, Short.MAX_VALUE)
					.addGap(18)
					.addComponent(btnAnnuler, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
					.addGap(87))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNumTache)
						.addComponent(lblValeurNumTache))
					.addGap(18)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE, false)
						.addComponent(txtLibelleTache, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblLibellé))
					.addGap(18)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblChargePrevi)
						.addComponent(txtChargeTache, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(cboCollaborateur, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblCollaborateur))
					.addGap(33)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(cboContrat, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblContrat))
					.addGap(18)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnAnnuler)
						.addComponent(btnValider))
					.addGap(30))
		);
		contentPane.setLayout(gl_contentPane);
		remplirCboCollaborateur();
		remplirCboContrat();
		remplirChamps();
	}
	
	private void remplirChamps() {
		JTable laTable = FenetrePrincipale.getTblTaches();
		int ligneSelectionnee = FenetrePrincipale.getTblTaches().getSelectedRow();
		int numeroTache = (int) laTable.getValueAt(ligneSelectionnee, 0);
		lblValeurNumTache.setText(Integer.valueOf(numeroTache).toString());
		int referenceContratTache = (int) laTable.getValueAt(ligneSelectionnee, 1);
		for(int i=0; i<cboContrat.getItemCount(); i++) {
			Contrat item = cboContrat.getItemAt(i);
			if(item.getReference()==referenceContratTache){
				cboContrat.setSelectedIndex(i);
				break;
			}
		}		
		int matriculeCollaborateur = (int) laTable.getValueAt(ligneSelectionnee, 2);
		for(int i=0; i<cboCollaborateur.getItemCount(); i++) {
			Collaborateur item =cboCollaborateur.getItemAt(i);
			if(item.getMatricule()==matriculeCollaborateur) {
				cboCollaborateur.setSelectedIndex(i);
				break;
			}
		}
		String libelleTache = (String) laTable.getValueAt(ligneSelectionnee, 3);
		txtLibelleTache.setText(libelleTache);
		int ChargeTache = (int) laTable.getValueAt(ligneSelectionnee, 4);
		txtChargeTache.setText(Integer.valueOf(ChargeTache).toString());		
	}
	
	@SuppressWarnings("unchecked")
	public void remplirCboCollaborateur() {
		lesCollaborateurs = FenetrePrincipale.getLesCollaborateurs();

		for (int i = 0; i < lesCollaborateurs.size(); i++) {
			cboCollaborateur.addItem(lesCollaborateurs.get(i));
		}
	}

	@SuppressWarnings("unchecked")
	public void remplirCboContrat() {
		lesContrats = FenetrePrincipale.getLesContrats();

		for (int i = 0; i < lesContrats.size(); i++) {
			cboContrat.addItem(lesContrats.get(i));
		}
	}
}
